#!/bin/bash  

target=$1
buildType=$2
branchName=$3
if [ ! $1 ]; then
    target='debug'
fi
if [ ! $2 ]; then
    buildType='full'
fi

project_path=$(cd `dirname $0`; pwd)

a=`uname  -a`
mac="Darwin"

if [[ $a =~ $mac ]];then
	sed -i "" "s#^COMPILE_SDK_VERSION_FOR_GG=.*#COMPILE_SDK_VERSION_FOR_GG=false#g"  ${project_path}/gradle.properties
	sed -i "" "s#^COMPILE_SDK_USE_GOOGLE_PUSH=.*#COMPILE_SDK_USE_GOOGLE_PUSH=false#g"  ${project_path}/gradle.properties
	sed -i "" "s#^GG_MAP=.*#GG_MAP=false#g"  ${project_path}/gradle.properties

else
    sed -i  "s#^COMPILE_SDK_VERSION_FOR_GG=.*#COMPILE_SDK_VERSION_FOR_GG=false#g"  ${project_path}/gradle.properties
    sed -i  "s#^COMPILE_SDK_USE_GOOGLE_PUSH=.*#COMPILE_SDK_USE_GOOGLE_PUSH=false#g"  ${project_path}/gradle.properties
    sed -i  "s#^GG_MAP=.*#GG_MAP=false#g"  ${project_path}/gradle.properties
fi
#使用方法

echo "  "
echo "=========================================================="
echo "==== Running Git Command ..."
echo "=========================================================="
date
git config --global --add safe.directory "*"
git rev-list HEAD --count


#app 将XXX替换成自己的名称
project_name=iCam365
if [ $buildType = "gg" ];then
project_name=iCam365_Google
if [[ $a =~ $mac ]];then
	sed -i "" "s#^COMPILE_SDK_VERSION_FOR_GG=.*#COMPILE_SDK_VERSION_FOR_GG=true#g"  ${project_path}/gradle.properties
	sed -i "" "s#^COMPILE_SDK_USE_GOOGLE_PUSH=.*#COMPILE_SDK_USE_GOOGLE_PUSH=true#g"  ${project_path}/gradle.properties
	sed -i "" "s#^GG_MAP=.*#GG_MAP=true#g"  ${project_path}/gradle.properties
else
    sed -i "s#^COMPILE_SDK_VERSION_FOR_GG=.*#COMPILE_SDK_VERSION_FOR_GG=true#g"  ${project_path}/gradle.properties
    sed -i "s#^COMPILE_SDK_USE_GOOGLE_PUSH=.*#COMPILE_SDK_USE_GOOGLE_PUSH=true#g"  ${project_path}/gradle.properties
    sed -i "s#^GG_MAP=.*#GG_MAP=true#g"  ${project_path}/gradle.properties
fi
fi

echo '///------------'
echo '/// 清理项目中...  '
echo '///------------'
echo ''
chmod +x ./gradlew
./gradlew clean 
echo '///------------'
echo '/// 打包'${buildType}_${target}'版本开始  '
echo '///------------'
echo ''

if [ $target = "debug" ];then
    ./gradlew assemble${buildType}${target} || EXIT_STATUS=$?
    if [[ $EXIT_STATUS -ne 0 ]]; then
           exit $EXIT_STATUS
    fi
else
    # 只在release版本开启资源混淆
    ./gradlew resguard${buildType}${target} || EXIT_STATUS_RELEASE=$?
    if [[ $EXIT_STATUS_RELEASE -ne 0 ]]; then
           exit $EXIT_STATUS_RELEASE
    fi
fi


echo '///-------------'
echo '///打包'${buildType}_${target}'版本结束  '
echo '///-------------'

# release版本要上传一下混淆映射文件到Maven服务器
if [ $target = "release" ];then
    echo '///-------------'
    echo '///'${buildType}_${target}' 需要上传混淆映射文件 '
    echo '///-------------'
    ./gradlew :app:packageMapping || EXIT_STATUS_PACK=$?
    if [[ EXIT_STATUS_PACK -ne 0 ]]; then
           exit EXIT_STATUS_PACK
    fi
    echo '///-------------'
    echo '///'${buildType}_${target}' 混淆映射文件 压缩完成'
    echo '///-------------'
    ./gradlew :app:publish --info || EXIT_STATUS_PUB=$?
    if [[ EXIT_STATUS_PUB -ne 0 ]]; then
           exit EXIT_STATUS_PUB
    fi
    echo '///-------------'
    echo '///'${buildType}_${target}' 混淆映射文件 发布完成'
    echo '///-------------'
fi

# Git 提交号
git_version=`git rev-list HEAD --count`

#时间
time=$(date "+%Y_%m_%d")
description=${project_name}"来自分支:"${branchName}"-来自提交:"${git_version}
echo $description
#build文件夹路径
build_path=${project_path}/app/build/outputs/apk/${buildType}/${target}/${project_name}_${target}.apk
#上传到pgyer
api_key="1cdad4511eaaafc93a2b0555fc8b8e9f"

# 谷歌包，上传到另外的地址
if [ $buildType = "gg" ];then
    api_key="795b63cc78d9ae7f1b0c5f7269c7887b"
fi

password="tange"
echo '///-------------'
echo '/// 上传中 '
echo '///-------------'
curl -F "file=@"${build_path} -F "buildPassword="${password} -F "buildUpdateDescription="${description} -F "buildInstallType=2" -F "_api_key="${api_key} https://www.pgyer.com/apiv2/app/upload
echo '///-------------'
echo '/// 发布完成 '
echo '///-------------'


exit 0

